<?php

class Dwoo_Plugin_text extends Dwoo_Plugin { 
	public function process($variable, $fieldName, $escape = 'html', $defaultValue = '', array $rest = array()) {
		$value = $this->dwoo->readVar($variable);
		$execContainer = $this->dwoo->readVarInto('container', $this->dwoo->getData());
		if(!$execContainer->getAttribute('isEditing', 'org.welcomescreen', false)) {
			if($escape !== false) {
				// load the plugin if it hasn't been loaded yet
				if(!function_exists('Dwoo_Plugin_escape')) {
					$this->dwoo->getLoader()->loadPlugin('escape', false);
				}
				$value = Dwoo_Plugin_escape($this->dwoo, $value, $escape);
			}
			return $value;
		} else {
			// template is displayed in wysiwyg edit mode so we render the 
			/*
			<script type="text/javascript">dojo.require("dijit.InlineEditBox");
			  dojo.require("dojo.parser");

			  function editableHeaderOnChange(id, arg){
			    alert("editableHeader changed with id " + id + " and arguments "+arg);
			  }
			</script><h3 id="editableHeader" dojoType="dijit.InlineEditBox" onChange="editableHeaderOnChange(this.id,arguments[0])" autoSave="true" title="company name"></h3>
			*/
			// onChange="parent.visitFormElementChanged(\"%s\", arguments[0]);" 
			if($value === null) {
				$value = $defaultValue;
			}
			$html = sprintf('<span dojoType="dijit.InlineEditBox" autoSave="true">
				<script type="dojo/method" event="onChange">
					parent.visitFormElementChanged("%s", arguments[0]);
				</script>
				%s
			</span>', addslashes($fieldName), htmlspecialchars($value, ENT_QUOTES, 'utf-8'));
			return $html;
		}
	} 
}

?>